package com.xlh.controller;

import com.xlh.entity.ParamSchool;
import com.xlh.imp.SchoolInfoActionImpl;
import com.xlh.util.PageDTO;
import com.xlh.util.UnicodeUtil;
import com.xlh.util.UtilFile;
import com.xlh.vo.SchoolInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/20.
 */
@RestController
@RequestMapping("/school")
@Api(tags = "SchoolAction", description = "登录平台主类")
public class SchoolAction {

    @Autowired
    private SchoolInfoActionImpl schoolInfoActionImpl;

    @Autowired
    private UtilFile utilFile;
    //查询学校详情信息
    @RequestMapping(value = "/querySchoolInfo", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> querySchoolInfo(@RequestParam String schoolName,@RequestParam Integer systemId ,@RequestParam( defaultValue = "1") Integer pageNum,
                                              @RequestParam( defaultValue = "10") Integer pageSize) {

        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        Map<String, Object> preParamMap= new HashMap<String,Object>();
        try {
            preParamMap.put("schoolName",schoolName);
            preParamMap.put("systemId",systemId);
            PageDTO pageInfo = schoolInfoActionImpl.querySchoolInfo(pageNum, pageSize, preParamMap);
            resultMap.put("data",pageInfo);
            resultMap.put("code",200);
            resultMap.put("result","success");
        } catch (Exception e) {
            resultMap.put("result","fail");
            resultMap.put("failMsg","系统异常");
            e.printStackTrace();
        }
        return resultMap;
    }
    //查询销售人员信息
    @RequestMapping(value = "/querySaleUser", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> querySaleUser(HttpServletResponse response,@RequestParam String saleId) {
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            List<Map<String, Object>> data  = schoolInfoActionImpl.querySaleUser(saleId);
            resultMap.put("data",data);
            resultMap.put("code",200);
            resultMap.put("result","success");
        } catch (Exception e) {
            resultMap.put("result","fail");
            resultMap.put("failMsg","系统异常");
            e.printStackTrace();
        }
        return resultMap;
    }


    //根据学校名称查询详情
    @ApiOperation("解码学校名称返回账户信息")
    @RequestMapping(value = "/queryDecodeSchoolInfoById", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> queryDecodeSchoolInfoById(@RequestParam String encodeSchoolName) throws Exception  {
        String schoolName = UnicodeUtil.decodeUnicode(encodeSchoolName);
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            SchoolInfoVO schoolInfoVO = schoolInfoActionImpl.querySchoolInfoById(schoolName);
            resultMap.put("result", "success");
            resultMap.put("code", 200);
            resultMap.put("data", schoolInfoVO);
        } catch (Exception e) {
            resultMap.put("result", "fail");
            resultMap.put("failMsg","系统异常");
            e.printStackTrace();
        }
        return resultMap;
    }



    //根据学校名称查询详情
    @RequestMapping(value = "/querySchoolInfoById", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> querySchoolInfoById(@RequestParam String schoolName) throws Exception  {

        Map<String,Object> resultMap = new HashMap<String,Object>();
            try {
            SchoolInfoVO schoolInfoVO = schoolInfoActionImpl.querySchoolInfoById(schoolName);
            resultMap.put("result", "success");
            resultMap.put("code", 200);
            resultMap.put("data", schoolInfoVO);
        } catch (Exception e) {
            resultMap.put("result", "fail");
            resultMap.put("failMsg","系统异常");
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 保存
     * @param paramSchool
     * @return
     */
    @ApiOperation("修改平台用户信息")
    @PostMapping(value = "/savePlatformUserInfo")
    public Map<String,Object> savePlatformUserInfo(@Valid @RequestBody ParamSchool paramSchool ) {

        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            schoolInfoActionImpl.savePlatformUserInfo(paramSchool);
            resultMap.put("result", "success");
            resultMap.put("code", 200);
        } catch (Exception e) {
            resultMap.put("result", "fail");
            resultMap.put("failMsg", e.getMessage());
            resultMap.put("code", 500);
            e.printStackTrace();
        }
        return resultMap;
    }

    //编码转化
    @RequestMapping(value = "/unicodeInfo", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> unicodeInfo(@RequestParam String saleId,@RequestParam String schoolName) throws Exception  {
        Map<String,Object> map=new HashMap<>();
        String saleIdzw = UnicodeUtil.decodeUnicode(saleId);
        String schoolNamezw = UnicodeUtil.decodeUnicode(schoolName);
        map.put("saleIdzw",saleIdzw);
        map.put("schoolNamezw",schoolNamezw);
        map.put("code", 200);
        return map;
    }

    @ApiOperation("新增学校信息")
    @RequestMapping(value = "/addSchoolInfo", method = RequestMethod.POST)
    public  Map<String,Object> addSchoolInfo(@RequestBody ParamSchool paramSchool ) throws IOException {

        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            //获取参数值
            Map<String, Object> map = schoolInfoActionImpl.addSchoolInfo(paramSchool);
            resultMap.put("result", "success");
            resultMap.put("data", map);
            resultMap.put("code", 200);
        } catch (Exception e) {
            resultMap.put("code", 500);
            resultMap.put("result", "fail");
            resultMap.put("failMsg","系统异常");
            e.printStackTrace();
        }
        return  resultMap;
    }

        /**
     * @description 跳转到转换页面
     * */
    @RequestMapping(value = "/skipConvertPage", method = RequestMethod.GET)
    public void skipConvertPage(@RequestParam String schoolName,@RequestParam String saleId,@RequestParam String salePhone,HttpServletResponse rep,RedirectAttributes model,HttpServletRequest request,@RequestParam Integer type)  {
        Map<String, Object> sessionMap = new HashMap<String, Object>();
        try {
            request.getSession().setAttribute("saleId", saleId);
            request.getSession().setAttribute("salePhone", salePhone);
            request.getSession().setAttribute("schoolName", schoolName);
            model.addFlashAttribute("saleId", saleId);
            model.addFlashAttribute("salePhone", salePhone);
            model.addFlashAttribute("schoolName", schoolName);
            model.addFlashAttribute("type", type);
            rep.sendRedirect(utilFile.getIndexAddress()+"saleId="+ saleId +"&salePhone="+salePhone+"&schoolName="+schoolName+"&type="+type);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
